package com.usac.arqui1.grupo2.proyecto1;

import java.util.Locale;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

import com.usac.arqui1.grupo2.proyecto1.app.utilGlobals;
import com.usac.arqui1.grupo2.proyecto1.menufragments.Editor;
import com.usac.arqui1.grupo2.proyecto1.menufragments.Efectos;

public class Principal extends FragmentActivity {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	/* Vista Previa Variables */
	// private Button VistaPrev;

	/* Efectos Variables */
	public int colorletra;
	public int colorfondo;
	public int tamaniol;
	public int efectoletra;
	public String cadena;
	public int cstparpadeo;
	public int prmparpadeo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_principal);

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment;
			switch (position) {
			case 0:
				fragment = new Editor();
				break;
			case 1:
				fragment = new Efectos();
				break;
			default:
				throw new IllegalArgumentException("Invalid section number");
			}

			Bundle args = new Bundle();

			fragment.setArguments(args);
			return fragment;
		}

		@Override
		public int getCount() {
			// Total 2 paginas
			return 2;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.title_section1).toUpperCase(l);
			case 1:
				return getString(R.string.title_section2).toUpperCase(l);
			}
			return null;
		}
	}

	// -------------------------------------------

	// ----------------------
	// METODOS DE EDITOR
	// ----------------------

	/* TODO AQUI METODOS DE EDITOR */

	// ----------------------
	// METODOS DE EFECTOS
	// ----------------------

	/* TODO AQUI METODOS DE EFECTOS */

	public void enviarAArduino(View view) {

		Spinner spletra;
		spletra = (Spinner) findViewById(R.id.spinnerletra);

		Spinner spfondo;
		spfondo = (Spinner) findViewById(R.id.spinnerfondo);

		Spinner spefecto;
		spefecto = (Spinner) findViewById(R.id.spinnerefectos);

		Spinner tamano;
		tamano = (Spinner) findViewById(R.id.spntam);

		Spinner tipo;
		tipo = (Spinner) findViewById(R.id.spntipo);

		EditText textoedit;
		textoedit = (EditText) findViewById(R.id.texto);

		CheckBox chkparpadeo;
		chkparpadeo = (CheckBox) findViewById(R.id.parpadeo);

		cstparpadeo = (chkparpadeo.isChecked()) ? 1 : 0;

		recibirDatosF3(spfondo.getSelectedItemPosition(),
				spletra.getSelectedItemPosition(),
				tamano.getSelectedItemPosition(), cstparpadeo,
				spefecto.getSelectedItemPosition(), textoedit.getText()
						.toString());
		

		Log.d("CadenaSalida", this.getCadenaSalida());

		 final utilGlobals globalVariable = (utilGlobals)
		 getApplicationContext();
		 globalVariable.conectaSocket();
		 globalVariable.sendMessage(this.getCadenaSalida());
	}

	public void verVistaPrevia(View view) {
		Spinner spletra;
		spletra = (Spinner) findViewById(R.id.spinnerletra);

		Spinner spfondo;
		spfondo = (Spinner) findViewById(R.id.spinnerfondo);

		Spinner spefecto;
		spefecto = (Spinner) findViewById(R.id.spinnerefectos);

		Spinner tamano;
		tamano = (Spinner) findViewById(R.id.spntam);

		Spinner tipo;
		tipo = (Spinner) findViewById(R.id.spntipo);

		EditText textoedit;
		textoedit = (EditText) findViewById(R.id.texto);

		CheckBox chkparpadeo;
		chkparpadeo = (CheckBox) findViewById(R.id.parpadeo);

		Intent i = new Intent(this, VistaPrevia.class);
		Bundle parametrosB = new Bundle();

		// Editor
		parametrosB.putString("texto", textoedit.getText().toString());
		parametrosB.putInt("tamano", tamano.getSelectedItemPosition());
		parametrosB.putInt("tipo", tipo.getSelectedItemPosition());
		// Efectos
		parametrosB.putInt("colorletra", spletra.getSelectedItemPosition());
		parametrosB.putInt("colorfondo", spfondo.getSelectedItemPosition());
		parametrosB.putInt("efectos", spefecto.getSelectedItemPosition());

		parametrosB.putBoolean("colorparpadeo", chkparpadeo.isChecked());

		i.putExtras(parametrosB);
		startActivity(i);

		cstparpadeo = (chkparpadeo.isChecked()) ? 1 : 0;

		recibirDatosF3(spfondo.getSelectedItemPosition(),
				spletra.getSelectedItemPosition(),
				tamano.getSelectedItemPosition(), cstparpadeo,
				spefecto.getSelectedItemPosition(), textoedit.getText()
						.toString());
	}

	/**
	 * Recibe parametros de los datos y los inicializa
	 * 
	 * @param colorf
	 * @param colorl
	 * @param tamanio
	 * @param parpadeo
	 * @param efecto
	 * @param texto
	 */

	public void recibirDatosF3(int colorf, int colorl, int tamanio,
			int parpadeo, int efecto, String texto) {
		colorfondo = colorf;
		colorletra = colorl;
		tamaniol = tamanio;
		prmparpadeo = parpadeo;
		efectoletra = efecto;
		cadena = texto;
	}

	/**
	 * Devuelve el String de los datos color fondo, color letra, tama�o,
	 * parpadeo fectos y texto
	 * 
	 * @return sendCadena
	 */
	public String getCadenaSalida() {

		String sendCadena = colorfondo + "" + colorletra + "" + tamaniol + ""
				+ prmparpadeo + "" + efectoletra + "" + cadena + "";
		return sendCadena;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}
}